<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    width="50%"
    draggable
    @close="closeDialog"
  >
    <el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
      <el-form-item label="所有者类型" prop="ownerType">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.ownerType"
          placeholder="请输入所有者类型"
          clearable
        />

        <span v-else>{{ formData.ownerType }}</span>
      </el-form-item>
      <el-form-item label="所有者主键" prop="owner主键">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.owner主键"
          placeholder="请输入所有者主键"
          clearable
        />

        <span v-else>{{ formData.owner主键 }}</span>
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.fileType"
          placeholder="请输入文件类型"
          clearable
        />

        <span v-else>{{ formData.fileType }}</span>
      </el-form-item>
      <el-form-item label="存储路径" prop="path">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.path"
          placeholder="请输入存储路径"
          clearable
        />

        <span v-else>{{ formData.path }}</span>
      </el-form-item>
      <el-form-item label="原始文件名" prop="originalfilename">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.originalfilename"
          placeholder="请输入原始文件名"
          clearable
        />

        <span v-else>{{ formData.originalfilename }}</span>
      </el-form-item>
      <el-form-item label="文件名" prop="filename">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.filename"
          placeholder="请输入文件名"
          clearable
        />

        <span v-else>{{ formData.filename }}</span>
      </el-form-item>
      <el-form-item label="文件地址" prop="url">
        <el-input
          v-if="!props.isReadonly"
          v-model="formData.url"
          placeholder="请输入文件地址"
          clearable
        />

        <span v-else>{{ formData.url }}</span>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="handleSubmit"
          >确 定</el-button
        >
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SYSANNEXESFILEADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
  }
);

const rules = reactive({
  ownerType: [{ required: true, message: "请输入所有者类型", trigger: "blur" }],
  owner主键: [{ required: true, message: "请输入所有者主键", trigger: "blur" }],
  fileType: [{ required: true, message: "请输入文件类型", trigger: "blur" }],
  path: [{ required: true, message: "请输入存储路径", trigger: "blur" }],
  originalfilename: [
    { required: true, message: "请输入原始文件名", trigger: "blur" },
  ],
  filename: [{ required: true, message: "请输入文件名", trigger: "blur" }],
  url: [{ required: true, message: "请输入文件地址", trigger: "blur" }],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  version: "",
  ownerType: "",
  owner主键: "",
  fileType: "",
  path: "",
  originalfilename: "",
  filename: "",
  url: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        version,
        ownerType,
        owner主键,
        fileType,
        path,
        originalfilename,
        filename,
        url,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      } = newVal;
      Object.assign(formData, {
        id,
        version,
        ownerType,
        owner主键,
        fileType,
        path,
        originalfilename,
        filename,
        url,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
      });
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.version = "";
  formData.ownerType = "";
  formData.owner主键 = "";
  formData.fileType = "";
  formData.path = "";
  formData.originalfilename = "";
  formData.filename = "";
  formData.url = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

/** 附件信息保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      await request({
        url: "/system/sysannexesfile/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table");
    }
  });
}

/** 附件信息查询详细表 */
async function getDatails() {
  const data = await request({
    url: "/system/sysannexesfile/getDetails",
    method: "post",
    data: formData,
  });
  console.log(data);
}

onMounted(() => {
  if (formData.id) {
    getDatails();
  }
});

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
